package workforce.limousine.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import limousine.util.SelectItem;
import limousine.vo.Users;
import limousine.util.Service;
import limousine.util.Token;
import limousine.util.Func;

public class RoleSystemsManagerEdit extends Action {

    private String errorMsg = "Process Fail";
    private String id = "";
    private String mode = "";
    private Func tool = new Func();

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession();
        System.out.println("***RoleSystemsManagerEdit");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
       /* if (!user.getRole_name().equals("Admin")) {
            System.out.println("--Not Admin Hack--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }*/


        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        service.setServletContext(this.getServlet().getServletContext());
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");


        DynaActionForm daf = (DynaActionForm) form;
        //	mode = request.getParameter("mode");

        //	if(mode.equals("1")){//edit mode
        id = request.getParameter("eid");
        System.out.println("EID " + id);
        //	}
        session.setAttribute(Token.ROLESYSTEMS_EID, id);

        List<SelectItem> rl = service.getRoleById(ds, id);//have one
        System.out.println("RL " + rl.size());
        if (rl.size() > 0) {

            session.setAttribute(Token.ROLESYSTEMS_ROLE_LIST, rl);
        }

//        List<SelectItem> sl = service.getAllSystems(ds);
        List<SelectItem> sl = service.getSystemsByEnable(ds,"1");
        if (sl.size() > 0) {
            String a = rl.get(0).getKey();
            String[] select_id = service.getSystemsIdByRoleId(ds, a);
            if (select_id != null) {
                for (int i = 0; i < sl.size(); i++) {
                    for (int j = 0; j < select_id.length; j++) {
                        if (sl.get(i).getKey().equals(select_id[j])) {
                            SelectItem si = new SelectItem();
                            si.setKey(sl.get(i).getKey());
                            si.setValue(sl.get(i).getValue());
                            si.setSelect(true);
                            sl.set(i, si);
                        }
                    }
                }
            }
            session.setAttribute(Token.ROLESYSTEMS_SYSTEMS_LIST, sl);
        }



        return mapping.findForward(Token.SUCCESS);

    }
}
